﻿using mediaControl.Event;
using mediaControl.Utils;
using mediaControl.Views;
using mediaControl.Vo;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Threading;
using MessageBox = System.Windows.MessageBox;

namespace mediaControl.ViewModels
{
    public class MainWindowViewModel : BindableBase, INavigationAware
    {
       
        private readonly IDialogService dialogService;

        private readonly IRegionManager regionManger;

        //事件聚合器
        private readonly IEventAggregator eventAggregator;

        private string title= "嘟答智能播放系统";

        private ProgramVo program;

        private string saveFlag = "0";

        private DispatcherTimer timer = new DispatcherTimer();


        public MainWindowViewModel(IDialogService dialogService,IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            this.dialogService = dialogService;
            this.regionManger = regionManager;
            NewProgramCommand = new DelegateCommand<String>(newProgram);
            CloseWindowCommand = new DelegateCommand(closeWindow);
            OpenProgramCommand = new DelegateCommand(openProgram);
            SaveProgramCommand = new DelegateCommand(saveProgram);
            CloseCommand = new DelegateCommand(closeWindow);
            SaveAsProgramCommand = new DelegateCommand(SaveAs);
            this.eventAggregator = eventAggregator;
            timer.Interval = TimeSpan.FromSeconds(30);
            timer.Tick += Timer_Tick;
            //eventAggregator.GetEvent<ProgramEvent>().Subscribe(Received);

        }

        private void SaveAs()
        {
            IDialogParameters parameters = new DialogParameters();
            parameters.Add("program", Program);
            dialogService.ShowDialog("saveAsProgram", parameters, SaveAsCallback);
        }

        private void SaveAsCallback(IDialogResult obj)
        {
            
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            SaveFlag = "0";
            eventAggregator.GetEvent<CloseWindowEvent>().Publish("0");
            timer.Stop();
        }

        private void saveProgram()
        {
            eventAggregator.GetEvent<SaveProgramEvent>().Publish();
            SaveFlag = "1";
            timer.Start();
            eventAggregator.GetEvent<CloseWindowEvent>().Publish("1");
            MessageBox.Show("保存成功！");
        }

        private void Received(ProgramVo obj)
        {
            //Title = "嘟答智能播放系统\\" + Program.Name + "\\" + Program.FilePath;
        }

        public DelegateCommand<String> NewProgramCommand{ get; set; }

        public DelegateCommand CloseWindowCommand { get; set; }

        public DelegateCommand OpenProgramCommand { get; set; }

        public DelegateCommand SaveProgramCommand { get; set; }

        public DelegateCommand SaveAsProgramCommand { get; set; }

        public DelegateCommand CloseCommand { get; set; }
        public string Title { get { return title; } set { this.title = value; RaisePropertyChanged(); } }

        public ProgramVo Program { get { return program; } set { this.program = value; RaisePropertyChanged(); } }

        public string SaveFlag { get { return saveFlag; } set { this.saveFlag = value; RaisePropertyChanged(); } }

        private void newProgram(String obj) {
            IDialogParameters parameters = new DialogParameters();
            dialogService.ShowDialog(obj, parameters, callback);
        }

        private void callback(IDialogResult result) {
            if (result.Result  == ButtonResult.OK) {
                Program = result.Parameters.GetValue<ProgramVo>("program");
                //Title = "嘟答智能播放系统\\" + Program.Name + "\\" + Program.FilePath;
                eventAggregator.GetEvent<ChangeTitleEvent>().Publish(Program);
                eventAggregator.GetEvent<ClearPlaySourceEvent>().Publish();
            }
        }

        private void closeWindow() {
            if ("0".Equals(SaveFlag)) {
                MessageBox.Show("请先保存项目！");
                return;
            }
            System.Windows.Application.Current.Shutdown();  // 关闭应用程序并终止所有实例
        }

        private void openProgram()

        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "选择文件";
            openFileDialog.Filter = "(*.info)|*.info";
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string foldPath = openFileDialog.FileName;
                string content = "";//返回的结果字符串
                using (StreamReader sr = new StreamReader(@foldPath))
                {
                    content = sr.ReadToEnd();//一次性把文本内容读完
                }
                JObject programInfo = JsonConvert.DeserializeObject<JObject>(content);
                Program = JsonConvert.DeserializeObject<ProgramVo>((String)programInfo.GetValue("programInfo"));
                //System.Windows.MessageBox.Show(content);
                //显示到列表
                //program.No = 0;
                eventAggregator.GetEvent<ProgramEvent>().Publish(Program);
                eventAggregator.GetEvent<ChangeTitleEvent>().Publish(Program);
                //新开项目清屏
                eventAggregator.GetEvent<ClearPlaySourceEvent>().Publish();
                WriteFileUtil.SaveOpenList(foldPath);
                //Title = "嘟答智能播放系统\\" + Program.Name + "\\" + Program.FilePath;

            }
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            regionManger.RequestNavigate("ProgramListRegion", "ProgramList");
            regionManger.RequestNavigate("ProgramSettingRegion", "ProgramSetting");
            regionManger.RequestNavigate("ScreenListRegion", "ScreenList");
            if (navigationContext.Parameters.ContainsKey("program"))
            {
                //取出传过来的值
                Program = navigationContext.Parameters.GetValue<ProgramVo>("program");
                eventAggregator.GetEvent<ProgramEvent>().Publish(Program);
            }

            if (navigationContext.Parameters.ContainsKey("screenIds"))
            {
                //取出传过来的值
                List<string> screenIds = navigationContext.Parameters.GetValue<List<string>>("screenIds");
                eventAggregator.GetEvent<OpenScreenEvent>().Publish(screenIds);
            }

            if (navigationContext.Parameters.ContainsKey("monitorVos"))
            {
                //取出传过来的值
                List<MonitorVo> monitorVos = navigationContext.Parameters.GetValue<List<MonitorVo>>("monitorVos");
                eventAggregator.GetEvent<GetMonitorEvent>().Publish(monitorVos);
            }

        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            
        }
    }
}
